#include "MiniBreakRules.h"


MiniBreakRules::MiniBreakRules()
{
	m_maxMoves = 100;
	m_piecesPerPlayer = 12;
}


MiniBreakRules::~MiniBreakRules()
{
}

bool MiniBreakRules::isLegal(int currentPlayer, std::vector< std::vector<char> >& grid, int fromX, int fromY, int toX, int toY)
{
	char pSymbol = 'p', opSymbol = 'P';
	if (currentPlayer == 1)	{
		pSymbol = 'P';
		opSymbol = 'p';
	}
	
	if (grid[toY][toX] == pSymbol) { // return false if square is occupied by a friendly piece
		return false; 
	} 
	
	if (fromX != toX) 
	{
		if (fromY == toY) { return false; } // Return false if the piece is being moved sideways, .
	}
	else if (fromY == toY) { // or not at all
		return false; 
	} 

	// If the piece is being moved more than one square or if the piece is being moved backwards...
	if (currentPlayer == 0 && ((toY-fromY) > 1 || toY < fromY)) { 
		return false; 
	}
	else if (currentPlayer == 1 && ((fromY-toY) > 1 || fromY < toY)) { 
		return false; 
	}

	return true; // Legal move.
}

void MiniBreakRules::initPlayers(std::vector< std::vector<char> >& grid, Player& p1, Player& p2)
{
	std::vector<Piece*> p1p;
	std::vector<Piece*> p2p;
	
	int i = 0;
	for (int x = 0; x < 6 && i < m_piecesPerPlayer; ++x)
	{
		for (int y = 0; y < 2; ++y) 
		{
			//Piece p('p', x, y);
			Piece *p = new Piece('p', x, y);
			p1p.push_back(p);
			grid[y][x] = p1p[i]->getSymbol();
			i++;
		}
	}
	
	i = 0;
	for (int x = 0; x < 6 && i < m_piecesPerPlayer; ++x)
	{
		for (int y = 4; y < 6; ++y) 
		{
			//Piece p('P', x, y);
			Piece *p = new Piece('P', x, y);
			p2p.push_back(p);
			grid[y][x] = p2p[i]->getSymbol();
		}
	}
	
	p1.setPieces(p1p);
	p2.setPieces(p2p);
}
